home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_11 / phillip2 / tiffs.c < prev    next >
C/C++ Source or Header  |  1993-06-07  |  36KB  |  1,272 lines

  1.  
  2.    /*************************** 
  3.    * 
  4.    *   tiffs.c 
  5.    *   COMPOSITE FILE COMPRISING: 
  6.    *   rtiff.c 
  7.    *   wtiff.c 
  8.    *   tiff.c 
  9.    * 
  10.    ***************************\ 
  11.  
  12.  
  13.  
  14.        /*********************************************
  15.        *
  16.        *  file d:\cips\rtiff.c
  17.        *
  18.        *  Functions: This file contains
  19.        *          read_tiff_image
  20.        *          read_line
  21.        *          seek_to_first_line
  22.        *          seek_to_end_of_line
  23.        *
  24.        *  Purpose:
  25.        *  These functions read a TIFF image and insert
  26.        *  the data into a ROWSxCOLS array of short.
  27.        *
  28.        *       NOTE: The fseek constants are
  29.        *             0=SEEK_SET = beginning of file
  30.        *             1=SEEK_CUR = current offset
  31.        *             2=SEEK_END = end of file
  32.        *
  33.        *  External Calls:
  34.        *  tiff.c - read_tiff_header
  35.        *
  36.        *  Modifications:
  37.        *       25 June 1990 - created
  38.        *       27 March 1993 - use fopen, fread, fseek
  39.        *           instead of the earlier open, read,
  40.        *           seek, etc.
  41.        *
  42.        **********************************************/
  43.  
  44. #include "cips.h"
  45.  
  46.  
  47.  
  48.  
  49.  
  50. read_tiff_image(image_file_name, array, il, ie, ll, le)
  51.       char   image_file_name[];
  52.       int    il, ie, ll, le;
  53.       short   array[ROWS][COLS];
  54. {
  55.    char  buffer[COLS],
  56.          rep[80];
  57.    int   bytes_read,
  58.          closed,
  59.          position,
  60.          i;
  61.    FILE  *image_file;
  62.    float a;
  63.    long  line_length, offset;
  64.  
  65.    struct tiff_header_struct image_header;
  66.  
  67.    read_tiff_header(image_file_name, &image_header);
  68.  
  69.       /***********************************************
  70.       *
  71.       *   Procedure:
  72.       *   Seek to the strip offset where the data begins.
  73.       *   Seek to the first line you want.
  74.       *   Loop over the lines you want to read:
  75.       *      Seek to the first element of the line.
  76.       *      Read the line.
  77.       *      Seek to the end of the data in that line.
  78.       *
  79.       ************************************************/
  80.  
  81.    image_file = fopen(image_file_name, "rb");
  82.    if(image_file != NULL){
  83.       position = fseek(image_file, 
  84.                        image_header.strip_offset, 
  85.                        SEEK_SET);
  86.       position = seek_to_first_line(image_file, 
  87.                                     &image_header, il);
  88.  
  89.       for(i=0; i<(ll-il); i++){
  90.          offset       = (ie-1)/
  91.                         (8/image_header.bits_per_pixel);
  92.          position     = fseek(image_file, offset, 
  93.                               SEEK_CUR);
  94.          bytes_read   = read_line(image_file, array, 
  95.                                   i, &image_header, 
  96.                                   ie, le);
  97.          position     = seek_to_end_of_line(image_file, 
  98.                                  le, &image_header);
  99.          position     = fseek(image_file, 1, 
  100.                               SEEK_CUR); 
  101.       }  /* ends loop over i  */
  102.  
  103.       closed = fclose(image_file);
  104.    }  /* ends if file opened ok */
  105.    else{
  106.       printf("\nRTIFF.C> ERROR - cannot open "
  107.              "tiff file");
  108.    }
  109.  
  110. }  /*  ends read_tiff_image */
  111.  
  112.  
  113.  
  114.  
  115.        /**********************************************
  116.        *
  117.        *   read_line(...
  118.        *
  119.        *   This function reads bytes from the TIFF 
  120.        *   file into a buffer, extracts the numbers 
  121.        *   from that buffer, and puts them into a 
  122.        *   ROWSxCOLS array of shorts. The process 
  123.        *   depends on the number of bits per pixel used 
  124.        *   in the file (4 or 8).
  125.        *
  126.        **********************************************/
  127.  
  128. read_line(image_file, array, line_number, 
  129.           image_header, ie, le)
  130.    FILE   *image_file;
  131.    int    ie, le, line_number;
  132.    short  array[ROWS][COLS];
  133.    struct tiff_header_struct *image_header;
  134. {
  135.    char  buffer[COLS], first, second;
  136.    float a, b;
  137.    int bytes_read, i;
  138.    unsigned int bytes_to_read;
  139.    union short_char_union scu;
  140.  
  141.    for(i=0; i<COLS; i++)
  142.       buffer[i] = '\0';
  143.  
  144.         /********************************************
  145.         *
  146.         *   Use the number of bits per pixel to 
  147.         *   calculate how many bytes to read.
  148.         *
  149.         ********************************************/
  150.  
  151.    bytes_to_read = (le-ie)/
  152.                    (8/image_header->bits_per_pixel);
  153.    bytes_read    = fread(buffer, 1, bytes_to_read, 
  154.                          image_file);
  155.  
  156.    for(i=0; i<bytes_read; i++){
  157.  
  158.         /*********************************************
  159.         *
  160.         *   Use unions defined in cips.h to stuff bytes
  161.         *   into shorts.
  162.         *
  163.         **********************************************/
  164.  
  165.       if(image_header->bits_per_pixel == 8){
  166.        scu.s_num          = 0;
  167.        scu.s_alpha[0]        = buffer[i];
  168.        array[line_number][i] = scu.s_num;
  169.       }  /* ends if bits_per_pixel == 8 */
  170.  
  171.       if(image_header->bits_per_pixel == 4){
  172.  
  173.        scu.s_num             = 0;
  174.        second                = buffer[i] & 0X000F;
  175.        scu.s_alpha[0]        = second;
  176.        array[line_number][i*2+1] = scu.s_num;
  177.  
  178.        scu.s_num             = 0;
  179.        first                 = buffer[i] >> 4;
  180.        first                 = first & 0x000F;
  181.        scu.s_alpha[0]        = first;
  182.        array[line_number][i*2] = scu.s_num;
  183.  
  184.       }  /* ends if bits_per_pixel == 4 */
  185.  
  186.    }  /*  ends loop over i  */
  187.  
  188.    return(bytes_read);
  189.  
  190. }  /* ends read_line  */
  191.  
  192.  
  193.  
  194.  
  195.  
  196.        /*********************************************
  197.        *
  198.        *   seek_to_first_line(...
  199.        *
  200.        **********************************************/
  201.  
  202. seek_to_first_line(image_file, image_header, il)
  203.    FILE   *image_file;
  204.    int    il;
  205.    struct tiff_header_struct *image_header;
  206. {
  207.    long offset;
  208.    int  position;
  209.  
  210.    offset   = (il-1)*image_header->image_width/
  211.              (8/image_header->bits_per_pixel);
  212.       /* seek from current position */
  213.    position = fseek(image_file, offset, SEEK_CUR);
  214.    return(position);
  215. }  /* ends seek_to_first_line */
  216.  
  217.  
  218.  
  219.  
  220.  
  221.        /**********************************************
  222.        *
  223.        *   seek_to_end_of_line(...
  224.        *
  225.        ***********************************************/
  226.  
  227. seek_to_end_of_line(image_file, le, image_header)
  228.    FILE   *image_file;
  229.    int    le;
  230.    struct tiff_header_struct *image_header;
  231. {
  232.    long  offset;
  233.    int   position;
  234.  
  235.    offset   = (image_header->image_width-le)/
  236.              (8/image_header->bits_per_pixel);
  237.    position = fseek(image_file, offset, SEEK_CUR);
  238.    return(position);
  239. }  /* ends seek_to_end_of_line         */
  240.  
  241.        /**********************************************
  242.        *
  243.        *  file d:\cips\wtiff.c
  244.        *
  245.        *  Functions: This file contains
  246.        *      create_file_if_needed
  247.        *      create_allocate_tiff_file
  248.        *      write_array_into_tiff_image
  249.        *      write_line
  250.        *      insert_short_into_buffer
  251.        *      insert_long_into_buffer
  252.        *      round_off_image_size
  253.        *      does_not_exist
  254.        *
  255.        *  Purpose:
  256.        *     These functions create TIFF image files 
  257.        *     on disk and insert a ROWSxCOLS array
  258.        *     into a tiff image already stored on disk.
  259.        *
  260.        *  External Calls:
  261.        *     rtiff.c - seek_to_first_line
  262.        *               seek_to_end_of_line
  263.        *     tiff.c - read_tiff_header
  264.        *
  265.        *  Modifications:
  266.        *     29 January 1991 - created
  267.        *     28 March 1993 - replaced open, lseek
  268.        *         etc. with fopen, fseek, etc.
  269.        *     10 May 1993 - added a number of tags
  270.        *         to make the TIFF files I create
  271.        *         TIFF 6.0 Gray Scale image compliant.
  272.        *
  273.        *********************************************/
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.        /**********************************************
  281.        *
  282.        *   create_file_if_needed(...
  283.        *
  284.        *   This function creates a file on disk
  285.        *   if it does not exist.  The out file is
  286.        *   patterned after the in file.
  287.        *